package com.example.md.mddemo.services;


import com.example.md.mddemo.entity.Person;
import com.example.md.mddemo.repo.PersonRepository;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * Copyright © 2018年 ziniuxiaozhu. All rights reserved.
 *
 * @Author 临江仙 hzqiuxm@163.com
 * TODO:亲爱的临江仙，请写点注释吧...........
 * @Date 2018/8/1 20:00
 */
@Service
public class PersonService {

    private static final Logger log = LoggerFactory.getLogger(PersonService.class);

    @Autowired
    private PersonRepository personRepository;

 /*   @Autowired
    private MongoTemplate mongoTemplate;*/

    /**
     *
     * @param person
     */
    @Transactional
    public void addPerson(Person person){

        personRepository.insert(person);
//        int a = 5/0;

    }


    /**
     *
     * @param objectId
     * @return
     */
    public Person findPerson(ObjectId objectId){

        Optional<Person> person = personRepository.findById(objectId);

        return person.get();


    /*    Person person = mongoTemplate.findOne(Query.query(where("_id").is(objectId)), Person.class);

        return person;*/

    }


    /**
     *
     * @param name
     * @return
     */
    public String findAddressByName(String name){

       return personRepository.findByName(name);

    }

    public List<Person> findByDes(String des){

        return personRepository.findByDesContains(des);

    }

}
